<link rel="import" href="../../../../packages/polymer/polymer.html">
<link rel="import" href="curly_block.html">
<link rel="import" href="observatory_element.html">
<link rel="import" href="service_ref.html">

<polymer-element name="instance-ref" extends="service-ref">
  <template>
    <link rel="stylesheet" href="css/shared.css">
    <style>
      .errorBox {
        background-color: #f5f5f5;
        border: 1px solid #ccc;
        padding: 10px;
        font-family: consolas, courier, monospace;
        font-size: 1em;
        line-height: 1.2em;
        white-space: pre;
      }
    </style>
    <span>
      <template if="{{ ref.isSentinel }}">
        <span title="{{ hoverText }}">{{ ref.valueAsString }}</span>
      </template>

      <template if="{{ ref.isBool || ref.isInt ||
                       ref.isDouble || ref.isNull }}">
        <a on-click="{{ goto }}" _href="{{ url }}">{{ ref.valueAsString }}</a>
      </template>

      <template if="{{ ref.isString }}">
        <a on-click="{{ goto }}" _href="{{ url }}">{{ asStringLiteral(ref.valueAsString, ref.valueAsStringIsTruncated) }}</a>
      </template>


      <template if="{{ ref.isAbstractType }}">
        <a on-click="{{ goto }}" _href="{{ url }}">{{ ref.name }}</a>
      </template>

      <template if="{{ ref.isClosure }}">
        <a on-click="{{ goto }}" _href="{{ url }}">
          {{ ref.closureFunc.qualifiedName }}
        </a>
      </template>

      <template if="{{ ref.isPlainInstance }}">
        <a on-click="{{ goto }}" _href="{{ url }}"><em>{{ ref.clazz.name }}</em></a>
        <curly-block callback="{{ expander() }}">
          <div class="memberList">
            <template repeat="{{ field in ref.fields }}">
              <div class="memberItem">
                <div class="memberName">
                  {{ field['decl'].name }}
                </div>
                <div class="memberValue">
                  <any-service-ref ref="{{ field['value'] }}"></any-service-ref>
                </div>
              </div>
            </template>
          </div>
        </curly-block>
      </template>

      <template if="{{ ref.isList }}">
        <a on-click="{{ goto }}" _href="{{ url }}"><em>{{ ref.clazz.name }}</em> ({{ ref.length }})</a>
        <curly-block callback="{{ expander() }}">
          <div class="memberList">
            <template repeat="{{ element in ref.elements }}">
              <div class="memberItem">
                <div class="memberName">[{{ element['index']}}]</div>
                <div class="memberValue">
                  <any-service-ref ref="{{ element['value'] }}"></any-service-ref>
                </div>
              </div>
            </template>
          </div>
        </curly-block>
      </template>

      <template if="{{ ref.isMirrorReference }}">
        <a on-click="{{ goto }}" _href="{{ url }}"><em>{{ ref.clazz.name }}</em></a>
        <curly-block callback="{{ expander() }}">
          <div class="memberList">
            <div class="memberItem">
              <div class="memberName">referent</div>
              <div class="memberValue">
                <any-service-ref ref="{{ ref.referent }}"></any-service-ref>
              </div>
            </div>
          </div>
        </curly-block>
      </template>

      <template if="{{ ref.isWeakProperty }}">
        <a on-click="{{ goto }}" _href="{{ url }}"><em>{{ ref.clazz.name }}</em></a>
        <curly-block callback="{{ expander() }}">
          <div class="memberList">
            <div class="memberItem">
              <div class="memberName">key</div>
              <div class="memberValue">
                <any-service-ref ref="{{ ref.key }}"></any-service-ref>
              </div>
            </div>
            <div class="memberItem">
              <div class="memberName">value</div>
              <div class="memberValue">
                <any-service-ref ref="{{ ref.value }}"></any-service-ref>
              </div>
            </div>
          </div>
        </curly-block>
      </template>
    </span>
  </template>
</polymer-element>

<script type="application/dart" src="instance_ref.dart"></script>
